% 3个数字的加减乘运算
clear;
% 页数
pagenum    = 4;
% 生成文档
m = 3;
n = 4;
GenDocument([], "mydoc", pagenum, m, n);

% 绘制表盘函数
function drawClock(ax)
    num1 = randi([0,11],1);
    num2 = randi([0,11],1);
    t = 0:0.1:2*pi+0.1;
    R = 0.9;
    l1 = 0.95;
    l2 = 0.9;
    l5 = 0.7;
    l3 = 0.6;
    l4 = 0.45;
    xs = R*cos(t);
    ys = R*sin(t);
    hold on;
    plot(ax, xs, ys,'k','LineWidth',1);
    for i = 1:12
        theta = deg2rad(-i*30+90);
        plot([R*cos(theta), l2*R*cos(theta)],[R*sin(theta), l2*R*sin(theta)],'k','LineWidth',1);
        h = text(l5*R*cos(theta),l5*R*sin(theta), num2str(i),'FontSize',6,"FontName","黑体",...
            'Rotation',-i*30,'HorizontalAlignment','center');
    end
    for i = 1:60
        theta = deg2rad(i*6+90);
        plot([R*cos(theta), l1*R*cos(theta)],[R*sin(theta), l1*R*sin(theta)],'k');
    end
    theta = deg2rad(90-num2*30);
    plot([0, l3*R*cos(theta)],[0, l3*R*sin(theta)],'k','LineWidth',1);
    dtheta = num2/12*30;
    theta = deg2rad(90-num1*30-dtheta);
    plot([0, l4*R*cos(theta)],[0, l4*R*sin(theta)],'k','LineWidth',2);
    axis equal;
end

% 绘制函数
function drawFunction(ax)
    drawClock(ax);
end
%% 以下是基础函数

% 生成图片
function GenerateImages(num, m, n)
    for i = 1:num
        a = subplot(m,n,1);
        cla;
        drawFunction(a);
        F = getframe(a);
        I = frame2im(F);
        imwrite(I,['p',num2str(i),'.png']);
    end
end

% 删除图片
function deletePngs(num)
    for i = 1:num
        delete(['p',num2str(i),'.png']);
    end
end

% 生成文档
function GenDocument(~, filename, pagenum, m, n)
% 参数
% problemstr 题目列表
% filename 文件名
% pagenum 页数
import mlreportgen.dom.*
d = Document(filename,"docx");
open(d);
cnt   = 1;
GenerateImages(12*pagenum, m, n);
tableDatas = cell(4,3,pagenum);
pidx = 0;
for i = 1:pagenum
    for j = 1:4
        for k = 1:3
            pidx = pidx + 1;
            tableDatas{j, k, i} = Image(['p',num2str(pidx),'.png']);
        end
    end
end
for i = 1:pagenum
    % 表格式
    tableStyles       = { ColSep("solid"), RowSep("solid"), Border("solid"), Width("100%"), RowHeight('140pt','atleast'), FontSize("12pt")};
    % 表头格式
    tableHeaderStyles = { BackgroundColor("lightgray"), Bold(true), FontSize("16pt"),RowHeight('20pt')};
    % 表头名称
    headerLabels      = ["开始：___","结束： ____" ,"得分： ____"];
    tableData = cell(4,3);
    for idx = 1:12
        [a,b] = ind2sub([4,3],idx);
        tableData{a,b} = tableDatas{a,b,i};
    end
    cnt       = cnt + 12;
    cellTbl   = FormalTable(headerLabels,tableData);
    cellTbl.Style = [cellTbl.Style, tableStyles];
    cellTbl.Header.Style = [cellTbl.Header.Style, tableHeaderStyles];
    cellTbl.TableEntriesInnerMargin = "2pt";
    append(d,cellTbl);
    if i ~= pagenum
        br = PageBreak();
        append(d,br);
    end
end
close(d);
deletePngs(12*pagenum);
end